package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.Result;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.ResultUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody @NotNull User u) {
        User userNode = userService.login(u);
        if (userNode != null) {
            return ResultUtil.success(userNode);
        }
        return ResultUtil.error(500, "登录失败");
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody @NotNull User u) {
        JSONObject jsonObject = userService.inert(u);
        User userNode = (User)jsonObject.get("data");
        if (userNode != null) {
            return ResultUtil.success(userNode);
        }
        return ResultUtil.error(500, (String)jsonObject.get("message"));
    }
}
